import 'package:flutter/material.dart';
import 'package:flutter_provider_demo/shoping_cart/model/shopping_cart_provider.dart';
import 'package:provider/provider.dart';

class ShoppingCartPage extends StatefulWidget {
  ShoppingCartPage({Key key}) : super(key: key);

  @override
  _ShoppingCartPageState createState() => _ShoppingCartPageState();
}

class _ShoppingCartPageState extends State<ShoppingCartPage> {
  @override
  Widget build(BuildContext context) {
    var textTheme = Theme.of(context).textTheme.headline3;

    var model = context.watch<ShoppingCartProvider>();

    return Scaffold(
      appBar: AppBar(
        title: Text('购物车'),
        centerTitle: true,
      ),
      body: Column(
        children: [
          Expanded(
            child: Padding(
              padding: const EdgeInsets.all(15.0),
              child: ListView.builder(
                itemCount: model.items.length,
                itemBuilder: (_, index) => ListTile(
                  leading: Icon(Icons.done),
                  trailing: IconButton(
                    icon: Icon(Icons.remove_circle_outline),
                    onPressed: () {
                      model.remove(model.items[index]);
                    },
                  ),
                  title: Text(
                    model.items[index].name,
                    style: Theme.of(context).textTheme.headline6,
                  ),
                ),
              ),
            ),
          ),
          Divider(height: 3, color: Colors.black),
          SizedBox(
            height: 160,
            child: Center(
              child: Consumer<ShoppingCartProvider>(
                builder: (_, model, child) => Text(
                  '\$${model.totalPrice}',
                  style: textTheme,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
